import ImageLoader from './ImageLoader';
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property({
        type: cc.Label,
        displayName: '用户名',
        tooltip: '用户名组件'
    })
    lblName: cc.Label = null;

    @property({
        type: cc.Label,
        displayName: '金币组件'
    })
    lblMoney: cc.Label = null;

    @property({
        type: cc.Label,
        displayName: '显示宝石的组件'
    })
    lblGems: cc.Label = null;

    @property({
        type: cc.Label,
        displayName: '显示ID的组件'
    })
    lblID: cc.Label = null;

    @property({
        type: cc.Label,
        displayName: '通告组件'
    })
    lblNotice: cc.Label = null;

    @property({
        type: cc.Node,
        displayName: '输入房间号组件'
    })
    joinGameWin: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: '建房间选项组件'
    })
    createRoomWin: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: '设置音效组件'
    })
    settingsWin: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: '帮助说明组件'
    })
    helpWin: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: '消息组件'
    })
    xiaoxiWin: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: '创建房间按钮'
    })
    btnJoinGame: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: '返回房间按钮'
    })
    btnReturnGame: cc.Node = null;

    @property({
        type: cc.Sprite,
        displayName: '用户头像'
    })
    sprHeadImg: cc.Sprite = null;

    onLoad() {
        if (!cc.sys.isNative && cc.sys.isMobile) {
            var cvs = this.node.getComponent(cc.Canvas);
            cvs.fitHeight = true;
            cvs.fitWidth = true;
        }
        if (!cc.vv) {
            cc.director.loadScene("loading");
            return;
        }
       
        this.initLabels();

        if(cc.vv.gameNetMgr.roomId == null){
            this.btnJoinGame.active = true;
            this.btnReturnGame.active = false;
        }
        else{
            this.btnJoinGame.active = false;
            this.btnReturnGame.active = true;
        }

        const roomId = cc.vv.userMgr.oldRoomId;
        if(roomId != null){
            cc.vv.userMgr.oldRoomId = null;
            cc.vv.userMgr.entryRoom(roomId);
        }

        const imgLoader = <ImageLoader>this.sprHeadImg.node.getComponent('ImageLoader');
        imgLoader.setUserID(cc.vv.userMgr.userId);
        cc.vv.utils.addClickEvent(this.sprHeadImg.node, this.node, 'Hall', 'onBtnClicked');
        
        this.addComponent('UserInfoShow');

        this.initButtonHandler('Canvas/right_bottom/btn_shezhi');
        this.initButtonHandler('Canvas/right_bottom/btn_help');
        this.initButtonHandler('Canvas/right_bottom/btn_xiaoxi');
        this.helpWin.addComponent('OnBack');
        this.xiaoxiWin.addComponent('OnBack');

        if(!cc.vv.userMgr.notice){
            cc.vv.userMgr.notice = {
                version: null,
                msg: '数据请求中...',
            };
        }

        if(!cc.vv.userMgr.gemstip){
            cc.vv.userMgr.gemstip = {
                version: null,
                msg: '数据请求中...',
            };
        }

        this.lblNotice.string = cc.vv.userMgr.notice.msg;

        this.refreshInfo();
        this.refreshNotice();
        cc.vv.audioMgr.playBGM('bgMain.mp3');
        cc.vv.utils.addEscEvent(this.node);
    }


    /**
     * 更新宝石信息
     */
    refreshGemsTip(){

        const onGet = (ret: cc.ret) => {
            if(ret.errcode !== 0){
                console.log('%c' + ret.errmsg, cc.vv.logStyle);
            }
            else{
                cc.vv.userMgr.gemstip.version = ret.version;
                cc.vv.userMgr.gemstip.msg = ret.msg.replace('<newline>', '\n');
            }
        };

        const data: cc.ret = {
            account: cc.vv.userMgr.account,
            sign: cc.vv.userMgr.sign,
            type: 'fkgm',
            version: cc.vv.userMgr.gemstip.version,
        };

        cc.vv.http.sendRequest('/get_message', data, onGet);
    }

    /**
     * 更新用户状态
     */
    refreshInfo(){

        const onGet = (ret: cc.ret) => {
            if(ret.errcode !== 0){
                console.log('%c' + ret.errmsg, cc.vv.logStyle);
            }
            else{
                if(ret.gems != null){
                    this.lblGems.string = ret.gems;
                }
            }
        };

        const data: cc.ret = {
            account: cc.vv.userMgr.account,
            sign: cc.vv.userMgr.sign,
        };
        //9001 client-service
        cc.vv.http.sendRequest('/get_user_status', data, onGet);
    }

    /**
     * 更新公告信息
     */
    refreshNotice(){

        const onGet = (ret: cc.ret) => {
            if(ret.errcode !== 0){
                console.log('%c' + ret.errmsg, cc.vv.logStyle);
            }
            else{
                cc.vv.userMgr.notice.version = ret.version;
                cc.vv.userMgr.notice.msg = ret.msg;
                this.lblNotice.string = ret.msg;

            }
        };

        const data: cc.ret = {
            account: cc.vv.userMgr.account,
            sign: cc.vv.userMgr.sign,
            type: 'notice',
            version: cc.vv.userMgr.notice.version,
        };
        //9001 hall_server/client_service.ts
        cc.vv.http.sendRequest('/get_message', data, onGet);
    }


    initButtonHandler(btnPath){
        const btn = cc.find(btnPath);
        cc.vv.utils.addClickEvent(btn, this.node, 'Hall', 'onBtnClicked');
    }


    /**
     * 初始化用户信息
     */
    initLabels(){
        console.log('%chall.initLabels()userName:' + cc.vv.userMgr.userName, cc.vv.logStyle);
        this.lblName.string = cc.vv.userMgr.userName;
        this.lblMoney.string = cc.vv.userMgr.conis.toString();
        this.lblGems.string = cc.vv.userMgr.gems;
        this.lblID.string = 'ID:' + cc.vv.userMgr.userId;
    }

    /**
     * 点击显示某个节点
     * @param event 
     */
    onBtnClicked(event: cc.Event.EventCustom){
        switch(event.target.name){
            case 'btn_shezhi':
                //显示设置信息界面
                this.settingsWin.active = true;
                break;
            case 'btn_help':
                //显示帮助信息界面
                this.helpWin.active = true;
                break;
            case 'btn_xiaoxi':
                //显示消息信息界面
                this.xiaoxiWin.active = true;
                break;
            case 'head':
                //显示头像信息界面
                cc.vv.userInfoShow.show(cc.vv.userMgr.userName, cc.vv.userMgr.userId, this.sprHeadImg, cc.vv.userMgr.sex, cc.vv.userMgr.ip);
                break;
        }
    }

    /**
     * 点击购买金币按钮（金币后面的+号）
     */
    onBtnAddGemsClicked(){
        cc.vv.alert.show('提示', cc.vv.userMgr.gemstip.msg, () => {
            this.onBtnTaobaoClicked();
        });
        this.refreshInfo();
    }

    /**
     * 打开购买金币的页面
     */
    onBtnTaobaoClicked(){
        cc.sys.openURL('https://购买金币的页面');
    }


    /**
     * 
     * @param dt 分享按钮
     */
    onShare(){
        cc.vv.anysdkMgr.share('分享标题', '分享内容11111');
    }


    /**
     * 创建房间按钮点击
     */
    onCreateRoomClicked(){
        if(cc.vv.gameNetMgr.roomId != null){
            cc.vv.alert.show('提示', '房间已经创建！\n必须解散当前房间才能创建新的房间！');
            return;
        }
        console.log('%c打开创建房间选项的界面', cc.vv.logStyle);
        this.createRoomWin.active = true;
    }


    /**
     * 加入房间按钮点击显示输入房间号界面
     */
    onJoinGameClicked(){
        this.joinGameWin.active = true;
    }

    /**
     * 返回房间按钮切换到游戏界面
     */
    onReturnGameClicked(){
        cc.vv.wc.show('正在返回游戏房间');
        cc.director.loadScene('mjgame');
    }

    /**
     * 打开反馈的页面
     */
    onBtnFeedbackClicked(){
        cc.sys.openURL('官网反馈的界面');
    }

    update(dt){
        //公告走马灯
        let x = this.lblNotice.node.x;
        x -= dt * 100;
        if(x + this.lblNotice.node.width < -1000){
            x = 500;
        }
        this.lblNotice.node.x = x;

        //断线重进
        if(cc.vv && cc.vv.userMgr.roomData != null){
            cc.vv.userMgr.entryRoom(cc.vv.userMgr.roomData);
            cc.vv.userMgr.roomData = null;
        }
    }
}
